package com.example.sale.Controller;

//
import com.example.sale.Result.R;
import com.example.sale.Service.IUserService;
import com.example.sale.True.User;
import com.example.sale.True.UserDetail;
import com.example.sale.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@CrossOrigin
@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private JwtUtil jwtUtil1;
    @PostMapping
    public R Post(@RequestBody User user){
        boolean usernameExists = iUserService.isUsernameExists(user.getUsername());
        if (usernameExists){
            return new R(true,false);
        }
        else {
            return new R(true,iUserService.save(user));
        }
    }
    @PostMapping("/login")
    public R PostLogin(@RequestBody User user,@RequestParam String key ){
        System.out.println(key);
        System.out.println(user.getVerCode());
        System.out.println(stringRedisTemplate.opsForValue().get(key));
        //判断验证码对不对
        if(!user.getVerCode().equals(stringRedisTemplate.opsForValue().get(key))){
            //如果不相等，说明验证不通过
            System.out.println("666");
            System.out.println(user.getVerCode());
            System.out.println(stringRedisTemplate.opsForValue().get(key));
            stringRedisTemplate.delete(key);
            System.out.println(stringRedisTemplate.opsForValue().get(key));
            System.out.println("777");
            return new R(false,"验证码错误");
        }
        Integer userExists = iUserService.isUserExists(user.getUsername(), user.getPassword(),user.getRole());
        if (userExists!=null){
            Map<String,String> payload = new HashMap<>();
            payload.put("id",String.valueOf(userExists));
            payload.put("username",user.getUsername());
            payload.put("role",user.getRole());
            //创建token
            String token =jwtUtil1.createToken(payload);
            //读取token
            User zhi = jwtUtil1.parseToken(token, User.class);
            log.info(token);
            log.info(String.valueOf(zhi));
            stringRedisTemplate.delete(key);
            return new R(true,token);
        }
        else {
            stringRedisTemplate.delete(key);
            return new R(false,false);
        }
    }
    @PostMapping("/test")
    public Map<String, Object> test(HttpServletRequest request){
        Map<String,Object>map = new HashMap<>();
        String token =request.getHeader("token");
        User zhi = jwtUtil1.parseToken(token,User.class);
        log.info(String.valueOf(zhi));
        map.put("state",true);
        map.put("msg","请求成功");
        return map;
    }
    @PostMapping("/ChangePassword")
    public Map<String, Object> Change(HttpServletRequest request,
                                      @RequestParam String OriginalPassword,
                                      @RequestParam String Password,
                                      @RequestParam String NewPassword)
    {
        System.out.println(OriginalPassword);
        System.out.println(Password);
        System.out.println(NewPassword);
        String token =request.getHeader("token");
        User zhi = jwtUtil1.parseToken(token,User.class);
        System.out.println(zhi);
        Integer id = zhi.getId();
        String password = iUserService.getPassword(id);
        System.out.println("你看看获取到的密码是什么"+password);
        System.out.println("你看看我们输入的密码是什么"+OriginalPassword);
        Map<String,Object> map1 =new HashMap<>();
        if(password.equals(OriginalPassword)){
            System.out.println(zhi);
            zhi.setPassword(Password);
            zhi.setInformation(false);
            System.out.println(zhi);
            Boolean flag =iUserService.updateById(zhi);
           if(flag == true){
               map1.put("status","true");
           }
           else{
               map1.put("status","false");
           }
           return map1;
        }
        map1.put("status","OriginalFalse");
        return map1;
    }
}